package com.qb.mobile.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qb.mobile.R;
import com.qb.mobile.model.Address;
import com.qb.mobile.model.Phonebook;

/**
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 *         RssListItem.java class
 * 
 */
public class PSListItem extends RelativeLayout implements Checkable {

	private boolean isChecked;
	private List<Checkable> checkableViews;

	public PSListItem(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		initialise(attrs);
	}

	public PSListItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialise(attrs);
	}

	public PSListItem(Context context, int checkableId) {
		super(context);
		initialise(null);
	}
	
	/*
	 * @see android.widget.Checkable#isChecked()
	 */
	public boolean isChecked() {
		return isChecked;
	}

	/**
	 * Read the custom XML attributes
	 */
	private void initialise(AttributeSet attrs) {
		this.isChecked = false;
		this.checkableViews = new ArrayList<Checkable>(5);
	}
	
	/*
	 * @see android.widget.Checkable#setChecked(boolean)
	 */
	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
		for (Checkable c : checkableViews) {
			c.setChecked(isChecked);
		}
	}
	
	/**
	 * @param phonebook
	 */
	public void setPhoneBookInfo(Phonebook phonebook) {
		if (null != phonebook) {
			TextView tvName = (TextView) findViewById(R.id.txtName);
			tvName.setText(phonebook.getName());

			TextView tvPhone = (TextView) findViewById(R.id.txtPhoneHome);
			tvPhone.setText(phonebook.getPhones().get(0).getNumber());
			
			TextView tvAddress = (TextView) findViewById(R.id.txtAddress);
			if(phonebook.getAddresses()!= null && !phonebook.getAddresses().isEmpty()){
				List<Address> listAddress = phonebook.getAddresses();
				tvAddress.setText(listAddress.get(0).toString());
			}else{
				tvAddress.setText("not found");
			}
			tvAddress.requestFocus();
			
			ImageView image = (ImageView) findViewById(R.id.image);
			image.setScaleType(ImageView.ScaleType.FIT_XY);
			if (phonebook.getPhoto() != null) {
				byte[] photoBlob = phonebook.getPhoto().getPhoto();
				Bitmap photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
				Drawable drawable = new BitmapDrawable(photoBitmap);
				image.setBackgroundDrawable(drawable);
			} else {
				image.setBackgroundResource(R.drawable.ic_contact_picture);
			}
			image.refreshDrawableState();
			image.requestFocus();
		}
	}

	@Override
	public void toggle() {
		this.isChecked = !this.isChecked;
		for (Checkable c : checkableViews) {
			c.toggle();
		}
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		final int childCount = this.getChildCount();
		for (int i = 0; i < childCount; ++i) {
			findCheckableChildren(this.getChildAt(i));
		}
	}
	
	/**
	 * Add to our checkable list all the children of the view that implement the
	 * interface Checkable
	 */
	private void findCheckableChildren(View v) {
		if (v instanceof Checkable) {
			this.checkableViews.add((Checkable) v);
		}

		if (v instanceof ViewGroup) {
			final ViewGroup vg = (ViewGroup) v;
			final int childCount = vg.getChildCount();
			for (int i = 0; i < childCount; ++i) {
				findCheckableChildren(vg.getChildAt(i));
			}
		}
	}
}
